﻿Common = Class.create();

Common.prototype = {
    initialize: function () {
        this.initCommonProperty();
    },

    initCommonProperty: function () {
        this.inputModel = new Object();

        this.NULL_DATE = "1900-01-01 00:00:00";

        this.dom = {};
    },
    //把传过来的inputModel，画面的id给画面控件赋值
    initInputForm: function (inputModel, prefix) {
        for (var key in inputModel) {
            if ($j("#" + prefix + "_" + key).attr("targetFormat")) {
                if (inputModel[key] != this.NULL_DATE) {
                    $j("#" + prefix + "_" + key).val(new Date(inputModel[key].replace(/-/g, "/")).format($j("#" + prefix + "_" + key).attr("targetFormat")));
                }
            }
            else {
                $j("#" + prefix + "_" + key).val(inputModel[key]);
            }
        }
    },
    //得到画面中做为where的值
    getSearchModel: function (prefix, className) {

        prefix = prefix == null ? "" : prefix;
        className = className == null ? "" : className;

        var searchModel = {};
        $j("input[type='text'][searchMode]", className).each(function () {
            searchModel[_getFieldName(this.id, prefix)] = this.value;
        });

        $j("input[type='hidden'][searchMode]", className).each(function () {
            searchModel[_getFieldName(this.id, prefix)] = this.value;
        });

        $j("input[type='password'][searchMode]", className).each(function () {
            searchModel[_getFieldName(this.id, prefix)] = this.value;
        });

        $j("select[searchMode]", className).each(function () {
            searchModel[_getFieldName(this.id, prefix)] = parseInt(this.value, 10);
        });

        $j("textarea[searchMode]", className).each(function () {
            searchModel[_getFieldName(this.id, prefix)] = this.value;
        });

        $j("input[type='radio'][searchMode]", className).each(function () {
            searchModel[_getFieldName(this.id, prefix)] = this.checked ? 1 : 0;
        });

        $j("input[type='checkbox'][searchMode]", className).each(function () {
            searchModel[_getFieldName(this.id, prefix)] = this.checked ? 1 : 0;
        });

        return searchModel;
    },
    //获取画面中所用的文本值
    getInputModel: function (inputModel) {
        if (MainScript.isNull(inputModel)) {
            inputModel = new Object();
        }

        return this.getModelByCssClass(inputModel, "");
    },
    //获取服务器控件的值
    getModelByCssClass: function (inputModel, className, prefix) {
        $j("input[type='text']", className).each(function () {
            if (this.id != "") {
                inputModel[_getFieldName(this.id, prefix)] = this.value;
            }
        });

        $j("input[type='hidden']", className).each(function () {
            if (this.id != "") {
                inputModel[_getFieldName(this.id, prefix)] = this.value;
            }
        });

        $j("input[type='password']", className).each(function () {
            if (this.id != "") {
                inputModel[_getFieldName(this.id, prefix)] = this.value;
            }
        });

        $j("select", className).each(function () {
            if (this.id != "") {
                inputModel[_getFieldName(this.id, prefix)] = parseInt(this.value, 10);
            }
        });

        $j("textarea", className).each(function () {
            if (this.id != "") {
                inputModel[_getFieldName(this.id, prefix)] = this.value;
            }
        });

        $j("input[type='radio']", className).each(function () {
            if (this.id != "") {
                inputModel[_getFieldName(this.id, prefix)] = this.checked ? 1 : 0;
            }
        });

        $j("input[type='checkbox']", className).each(function () {
            if (this.id != "") {
                inputModel[_getFieldName(this.id, prefix)] = this.checked ? 1 : 0;
            }
        });

        return inputModel;
    },
    //清空页面中文本的值
    clearForm: function (prefix) {
        //不是服务器控件
        if (prefix == null) {
            $j("input[type='text']").each(function () {
                this.value = "";
            });

            $j("input[type='hidden']").each(function () {
                this.value = "";
            });

            $j("input[type='password']").each(function () {
                this.value = "";
            });

            $j("select").each(function () {
                $j(this).find("option").eq(0).attr("selected", "selected");
            });

            $j("textarea").each(function () {
                this.value = "";
            });

            $j("input[type='radio']").each(function () {

            });

            $j("input[type='checkbox']").each(function () {

            });
        }
            //服务器控件
        else {
            $j("input[type='text']").each(function () {
                if ($(this).id.indexOf(prefix) == 0) {
                    this.value = "";
                }
            });

            $j("input[type='hidden']").each(function () {
                if ($(this).id.indexOf(prefix) == 0) {
                    this.value = "";
                }
            });

            $j("input[type='password']").each(function () {
                if ($(this).id.indexOf(prefix) == 0) {
                    this.value = "";
                }
            });

            $j("select").each(function () {
                if ($(this).id.indexOf(prefix) == 0) {
                    $j(this).find("option").eq(0).attr("selected", "selected");
                }
            });

            $j("textarea").each(function () {
                if ($(this).id.indexOf(prefix) == 0) {
                    this.value = "";
                }
            });

            $j("input[type='radio']").each(function () {

            });

            $j("input[type='checkbox']").each(function () {

            });
        }
    },
    //发送Ajax参数传Model
    sendAjaxByModel: function (url, inputModel, callback) {

        var data = this.toJSON(inputModel);
        if (data == undefined || data == "") {
            data = {};
        }

        $j.ajax({
            type: "post",
            url: url,
            dataType: "text",
            data: { ajaxData: data },
            error: function (XMLHttpRequest, textStatus, errorThrown) {
            },
            success: function (data) {
                callback(data);
            }
        });
    },

    //发送Ajax参数是一个集合，比如同事删除id为 1、2、3的数据
    sendAjaxByList: function (url, list, callback) {
        $j.ajax({
            type: "post",
            url: url,
            data: { listKey: list },
            error: function (XMLHttpRequest, textStatus, errorThrown) {
            },
            success: function (data) {
                callback(data);
            }
        });
    },
    //发送Ajax参数带有查询条件和分页信息
    sendAjaxBySearchCondition: function (url, inputModel, callback) {

        var data = this.toJSON(inputModel);

        $j.ajax({
            type: "post",
            url: url,
            dataType: "text",
            data: inputModel,
            error: function (XMLHttpRequest, textStatus, errorThrown) {
            },
            success: function (data) {
                callback(data);
            }
        });
    },
    //获取页面的文本值，和分页信息
    getListCondition: function (searchCondition, pager) {
        var listCondition = {};

        listCondition.SearchCondition = searchCondition;

        listCondition.ListPager = {};

        listCondition.ListPager.PageIndex = pager.pageIndex;
        listCondition.ListPager.PageSize = pager.pageSize;
        listCondition.ListPager.SortColumn = pager.sortColumn;
        listCondition.ListPager.SortMode = pager.sortMode;


        return listCondition;

    },
    //把和面的Data转换成json
    jsonParse: function (jsonString) {
        if (MainScript.isNull(jsonString) || $j.trim(jsonString) == "") {
            return new Object();
        }
        jsonString = jsonString.replace(new RegExp('(^|[^\\\\])\\"\\\\/Date\\(-?([0-9]+)\\)\\\\/\\"', 'g'), "$1new Date(($2)).format('yyyy/MM/dd HH:mm:ss')");
        return eval("(" + jsonString + ")");
    },

    toJSON: function (jsonObject) {
        var S = [];
        var J = "";
        var T = "";
        //配列の場合
        if (Object.prototype.toString.apply(jsonObject) === '[object Array]') {
            for (var i = 0; i < jsonObject.length; i++) {
                S.push(this.toJSON(jsonObject[i]));
            }
            J = '[' + S.join(',') + ']';
        }
            //日付の場合
        else if (Object.prototype.toString.apply(jsonObject) === '[object Date]') {
            J = "\\/Date(" + jsonObject.getTime() + ")\\/";
        }
            //Functionの場合
        else if (Object.prototype.toString.apply(jsonObject) === '[object RegExp]' || Object.prototype.toString.apply(jsonObject) === '[object Function]') {
            J = jsonObject.toString();
        }
            //対象の場合
        else if (Object.prototype.toString.apply(jsonObject) === '[object Object]') {
            if (jsonObject == null) {
                return null;
            }
            for (var i in jsonObject) {
                //文字列の場合
                if (typeof (jsonObject[i]) == 'string') {
                    var value = jsonObject[i];
                    value = value.replaceAll("\\\\", "\\\\");
                    value = value.replaceAll('"', '\\"')
                    value = MainScript.encodeValue(value);

                    T = '"' + value + '"';
                }
                    //対象の場合
                else if (typeof (jsonObject[i]) === 'object') {
                    T = this.toJSON(jsonObject[i]);
                }
                    //その以外の場合
                else {
                    T = '"' + jsonObject[i] + '"';
                }

                if (typeof T == "string") {
                    if (T.indexOf("Date(") > 0) {
                        T = "\\" + T + "\\";
                    }
                }

                S.push('"' + i + '":' + T);
            }
            J = '{' + S.join(',') + '}';
        }
        else if (Object.prototype.toString.apply(jsonObject) === '[object String]') {
            J = "\"" + jsonObject + "\"";
        }
        else if (Object.prototype.toString.apply(jsonObject) === '[object Number]') {
            J = "\"" + jsonObject.toString() + "\"";
        }

        return J;
    }
}

Date.prototype.format = function (formatStr) {
    var date = this;
    /*  
    函数：填充0字符  
    参数：value-需要填充的字符串, length-总长度  
    返回：填充后的字符串  
    */
    var zeroize = function (value, length) {
        if (!length) {
            length = 2;
        }
        value = new String(value);
        for (var i = 0, zeros = ''; i < (length - value.length) ; i++) {
            zeros += '0';
        }
        return zeros + value;
    };
    return formatStr.replace(/"[^"]*"|'[^']*'|\b(?:d{1,4}|M{1,4}|yy(?:yy)?|([hHmstT])\1?|[lLZ])\b/g, function ($0) {
        switch ($0) {
            case 'd': return date.getDate();
            case 'dd': return zeroize(date.getDate());
            case 'ddd': return ['Sun', 'Mon', 'Tue', 'Wed', 'Thr', 'Fri', 'Sat'][date.getDay()];
            case 'dddd': return ['Sunday', 'Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday'][date.getDay()];
            case 'M': return date.getMonth() + 1;
            case 'MM': return zeroize(date.getMonth() + 1);
            case 'MMM': return ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec'][date.getMonth()];
            case 'MMMM': return ['January', 'February', 'March', 'April', 'May', 'June', 'July', 'August', 'September', 'October', 'November', 'December'][date.getMonth()];
            case 'yy': return new String(date.getFullYear()).substr(2);
            case 'yyyy': return date.getFullYear();
            case 'h': return date.getHours() % 12 || 12;
            case 'hh': return zeroize(date.getHours() % 12 || 12);
            case 'H': return date.getHours();
            case 'HH': return zeroize(date.getHours());
            case 'm': return date.getMinutes();
            case 'mm': return zeroize(date.getMinutes());
            case 's': return date.getSeconds();
            case 'ss': return zeroize(date.getSeconds());
            case 'l': return date.getMilliseconds();
            case 'll': return zeroize(date.getMilliseconds());
            case 'tt': return date.getHours() < 12 ? 'am' : 'pm';
            case 'TT': return date.getHours() < 12 ? 'AM' : 'PM';
        }
    });
}
//转换拼接控件id
function _getFieldName(domID, prefix) {
    if (MainScript.isNullOrEmpty(prefix)) {
        prefix = "";
    }
    else {
        prefix = prefix + "_";
    }
    return domID.replace(prefix, "");
}

String.prototype.replaceAll = function (src, des) {
    return this.replace(new RegExp(src, "gm"), des);
}